<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <!-- 组织架构主体 -->
      <el-card class="tree-card">
        <!-- 头部内容 -->
        <tree-tools @addDepts="addDepts" :tree-node="company" :isRoot="true" />
        <!-- 树形主体结构 -->
        <el-tree default-expand-all :data="departs" :props="defaultProps">
          <!-- 显示右侧操作结构 -->
          <tree-tools
            @delDepts="getDepartments"
            @addDepts="addDepts"
            @editDepts="editDepts"
            slot-scope="{ data }"
            :tree-node="data"
          />
        </el-tree>
      </el-card>
    </div>
    <!-- 新增部门弹层 -->
    <add-dept
      @addDepts="getDepartments"
      :show-dialog="showDialog"
      :tree-node="node"
      :showDialog.sync="showDialog"
      ref="addDept"
    />
  </div>
</template>

<script>
import TreeTools from "./components/tree-tools.vue";
import addDept from "./components/add-dept.vue";
import { getDepartments } from "@/api/departments";
import { transListToTreeData } from "@/utils";
export default {
  name: "My-Departments",
  components: {
    TreeTools,
    addDept,
  },
  data() {
    return {
      loading: false,
      company: { name: "Vector科技有限公司", manager: "负责人", id: "" },
      departs: [],
      defaultProps: {
        label: "name",
        children: "children",
      },
      // 添加部门控件
      showDialog: false,
      // 存储当前操作的节点
      node: null,
    };
  },
  created() {
    this.getDepartments();
  },
  methods: {
    // 编辑信息
    async editDepts(node) {
      // 调用addDept组件的getDepartDetail方法
      await this.$refs.addDept.getDepartDetail(node.id);
      // 打开弹窗
      this.showDialog = true;
      // 记录点击的部门
      this.node = node;
    },
    // 打开弹窗，添加子部门
    addDepts(node) {
      this.showDialog = true;
      // 记录当前操作部门，为添加子部门
      this.node = node;
    },
    // 获取树形结构数据
    async getDepartments() {
      this.loading = true;
      const { depts } = await getDepartments();
      this.departs = transListToTreeData(depts, "");
      setTimeout(() => {
        this.loading = false;
      }, 500);
    },
  },
};
</script>

<style scoped >
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>